package com.ruoyi.web.controller.gcgl;

import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.gcgl.domain.GcCustomer;
import com.ruoyi.gcgl.domain.GcSupply;
import com.ruoyi.gcgl.domain.GysptPurchase;
import com.ruoyi.gcgl.service.IGcSupplyService;
import com.ruoyi.gcgl.service.IGysptPurchaseService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 采购单Controller
 * 
 * @author ruoyi
 * @date 2025-09-06
 */
@Controller
@RequestMapping("/gcgl/purchase")
public class GysptPurchaseController extends BaseController
{
    private String prefix = "sj/purchase";

    @Autowired
    private IGysptPurchaseService gysptPurchaseService;
    @Autowired
    private IGcSupplyService gcSupplyService;

    @RequiresPermissions("gcgl:purchase:view")
    @GetMapping()
    public String purchase()
    {
        return prefix + "/purchase";
    }

    /**
     * 查询采购单列表
     */
    @RequiresPermissions("gcgl:purchase:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GysptPurchase gysptPurchase)
    {
        startPage();
        gysptPurchase.setDeleteFlag(1);
        List<GysptPurchase> list = gysptPurchaseService.selectGysptPurchaseList(gysptPurchase);
        return getDataTable(list);
    }

    /**
     * 导出采购单列表
     */
    @RequiresPermissions("gcgl:purchase:export")
    @Log(title = "采购单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GysptPurchase gysptPurchase)
    {
        List<GysptPurchase> list = gysptPurchaseService.selectGysptPurchaseList(gysptPurchase);
        ExcelUtil<GysptPurchase> util = new ExcelUtil<GysptPurchase>(GysptPurchase.class);
        return util.exportExcel(list, "采购单数据");
    }

    /**
     * 新增采购单
     */
    @GetMapping("/add")
    public String add(ModelMap mmap){
    	mmap.put("startAt", new Date());
    	mmap.put("owner", getLoginName());
    	mmap.put("ownerId", getUserId());
    	mmap.put("deptName", getSysUser().getDept().getDeptName());
    	mmap.put("deptId", getSysUser().getDept().getDeptId());
    	
    	GcSupply gcSupply = new GcSupply();
    	gcSupply.setDeleteFlag(1);
    	List<GcSupply> gcSupplylis = gcSupplyService.selectGcSupplyList(gcSupply);
    	mmap.put("sylis", gcSupplylis);
        return prefix + "/add";
    }

    /**
     * 新增保存采购单
     */
    @RequiresPermissions("gcgl:purchase:add")
    @Log(title = "采购单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GysptPurchase gysptPurchase)
    {
    	GcSupply supply = gcSupplyService.selectGcSupplyById(Long.parseLong(gysptPurchase.getSupplyId().toString()));
    	if(supply!=null){
    		gysptPurchase.setSupplyName(supply.getName());
    	}
    	gysptPurchase.setStatus(0);
    	gysptPurchase.setOwneId(getUserId());
    	gysptPurchase.setOwner(getLoginName());
    	gysptPurchase.setDept(getSysUser().getDept().getDeptName());
    	gysptPurchase.setDeptId(getSysUser().getDeptId());
    	gysptPurchase.setDeleteFlag(1);
    	gysptPurchase.setCreateAt(new Date());
    	gysptPurchase.setCreateUser(getLoginName());
    	gysptPurchase.setUpdateAt(new Date());
    	gysptPurchase.setUpdateUser(getLoginName());
        return toAjax(gysptPurchaseService.insertGysptPurchase(gysptPurchase));
    }

    /**
     * 修改采购单
     */
    @RequiresPermissions("gcgl:purchase:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        GysptPurchase gysptPurchase = gysptPurchaseService.selectGysptPurchaseById(id);
        mmap.put("gysptPurchase", gysptPurchase);
        
        GcSupply gcSupply = new GcSupply();
    	gcSupply.setDeleteFlag(1);
    	List<GcSupply> gcSupplylis = gcSupplyService.selectGcSupplyList(gcSupply);
    	mmap.put("sylis", gcSupplylis);
        return prefix + "/edit";
    }

    /**
     * 修改保存采购单
     */
    @RequiresPermissions("gcgl:purchase:edit")
    @Log(title = "采购单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GysptPurchase gysptPurchase)
    {
    	GcSupply supply = gcSupplyService.selectGcSupplyById(Long.parseLong(gysptPurchase.getSupplyId().toString()));
    	if(supply!=null){
    		gysptPurchase.setSupplyName(supply.getName());
    	}
    	gysptPurchase.setUpdateAt(new Date());
    	gysptPurchase.setUpdateUser(getLoginName());
        return toAjax(gysptPurchaseService.updateGysptPurchase(gysptPurchase));
    }

    /**
     * 删除采购单
     */
    @RequiresPermissions("gcgl:purchase:remove")
    @Log(title = "采购单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(gysptPurchaseService.deleteGysptPurchaseByIds(ids));
    }
}
